﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;


namespace NDK.Module.DAL
{
    class BoolParamServices
    {
        /// <summary>
        /// 窗体对象实例，单例模式,保证整个工程只有一个实例
        /// </summary>
        private static BoolParamServices _instance;
        private static readonly object singletonLock = new object();
        public static BoolParamServices Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new BoolParamServices();
                        }
                    }
                }
                return _instance;
            }
        }

        #region  Method
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
		{
		return SQLiteUtils.GetMaxID("ID", "BoolParams"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from BoolParams");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

			return SQLiteUtils.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(BoolParam model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into BoolParams(");
			strSql.Append("ID,参数名称,布尔值)");
			strSql.Append(" values (");
			strSql.Append("@ID,@参数名称,@布尔值)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4),
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@布尔值", DbType.Boolean)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.参数名称;
			parameters[2].Value = model.布尔值;

			SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BoolParam model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update BoolParams set ");
			strSql.Append("参数名称=@参数名称,");
			strSql.Append("布尔值=@布尔值");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@布尔值", DbType.Boolean),
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = model.参数名称;
			parameters[1].Value = model.布尔值;
			parameters[2].Value = model.ID;

			int rows=SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from BoolParams ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

			int rows=SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from BoolParams ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=SQLiteUtils.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BoolParam GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,参数名称,布尔值 from BoolParams ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

			BoolParam model=new BoolParam();
			DataSet ds=SQLiteUtils.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				model.参数名称=ds.Tables[0].Rows[0]["参数名称"].ToString();
				if(ds.Tables[0].Rows[0]["布尔值"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["布尔值"].ToString()=="1")||(ds.Tables[0].Rows[0]["布尔值"].ToString().ToLower()=="true"))
					{
						model.布尔值=true;
					}
					else
					{
						model.布尔值=false;
					}
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BoolParam GetModel(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,布尔值 from BoolParams ");
            strSql.Append(" where 参数名称=@name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String,50)};
            parameters[0].Value = name;

            BoolParam model = new BoolParam();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.参数名称 = ds.Tables[0].Rows[0]["参数名称"].ToString();
                if (ds.Tables[0].Rows[0]["布尔值"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["布尔值"].ToString() == "1") || (ds.Tables[0].Rows[0]["布尔值"].ToString().ToLower() == "true"))
                    {
                        model.布尔值 = true;
                    }
                    else
                    {
                        model.布尔值 = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,参数名称,布尔值 ");
			strSql.Append(" FROM BoolParams ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteUtils.Query(strSql.ToString());
		}

		

		#endregion  Method
    }
}
